﻿/*********************************************************************
 * CopyRight (c) 2009-2014 HangJing Teconology. All Rights Reserved.
 * FileName : 
 * Function : 
 * Created by jijunjian at 2014-10-23 19:02:38.
 * E-Mail   : jijunjian@ihangjing.com
*********************************************************************/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


using Hangjing.Model;
using Hangjing.SQLServerDAL;
using Hangjing.Common;
using System.Data.SqlClient;
using System.Data;
using Hangjing.DBUtility;

namespace Hangjing.SQLServerDAL.serverinterface
{
    /// <summary>
    /// 骑士相关接口实现
    /// </summary>
    public class DeliverAPP : IAPP
    {
        /// <summary>
        /// app登录返回用户编号
        /// </summary>
        /// <param name="UserName">用户名</param>
        /// <param name="Password">密码</param>
        /// <param name="sessionid">app链接时生成的sessionid</param>
        /// <param name="usertpe">1骑士，2商家，3站点，4取餐员</param>
        /// <returns></returns>
        public int APPLogin(string UserName, string Password, string sessionid)
        {
            int APPUserID = 0;
            DeliverInfo model = GetModelByUserNameAndPassword(UserName, Password, sessionid);
            if (model != null)
            {
                APPUserID = model.DataId;
            }

            return APPUserID;
        }

        /// <summary>
        /// 获取骑士登录时返回的json（这样的目前是让服务器程序只依赖引用的dll）
        /// </summary>
        /// <param name="model">配送员对像</param>
        /// <param name="state">1表示正常登录，-2 表示踢下线，-1表示失败</param>
        /// <returns></returns>
        public string getLoginJSON(int userid, string state)
        {
            DeliverInfo model = new Deliver().GetModel(userid);
            OrderCountInfo countinfo = new Custorder().SendDeliverOrderCount(model.DataId.ToString());
            OrderCountInfo expressinfo = new ExpressOrder().SendDeliverOrderCount(model.DataId.ToString());

            string msg = "Login::{\"userid\":\"" + model.DataId.ToString() + "\",\"state\":\"" + state + "\",\"workstate\":\"" + model.CodeId + "\",\"jobstate\":\"" + model.workstate + "\",\"name\":\"" + model.Name + "\",\"newordercount\":\"" + countinfo.CountIntValue + "\",\"ExpressOrdercount\":\"" + expressinfo.CountIntValue + "\",\"gid\":\"" + model.GpsIMEI + "\",\"cityid\":\"" + model.Inve1 + "\",\"sessionid\":\"" + model.sessionid + "\",\"havenotice\":\"" + (new DeliverGroup().checkDeliverNotice(userid)) + "\"}";
            return msg;
        }
        
        /// <summary>
        /// 骑士根据sessionid更新app登入信息,登录时设置1，退出设置0
        /// </summary>
        /// <param name="TogoNum"></param>
        /// <param name="NewPwd"></param>
        /// <returns></returns>
        public int UpdateLoginState(string sessionid, int State)
        {
            StringBuilder str = new StringBuilder();
            str.Append("update Deliver set Status=@State,OrderNum = 0 where sessionid=@sessionid");

            SqlParameter[] Para = 
            {
                new SqlParameter("@sessionid",SqlDbType.VarChar,256),
                new SqlParameter("@State",SqlDbType.Int)
            };
            Para[0].Value = sessionid;
            Para[1].Value = State;

            return SQLHelper.ExecuteNonQuery(CommandType.Text, str.ToString(), Para);
        }

        /// <summary>
        /// 配送员登录,更新状态，存储sessionid
        /// </summary>
        /// <param name="UserName"></param>
        /// <param name="Password"></param>
        /// <param name="sessionid"></param>
        /// <returns></returns>
        public DeliverInfo GetModelByUserNameAndPassword(string UserName, string Password, string sessionid)
        {
            SqlParameter[] parameters = 
            {
                new SqlParameter("@UserName" , SqlDbType.VarChar , 50),
                new SqlParameter("@Password" , SqlDbType.VarChar ,50),
                new SqlParameter("@sessionid" , SqlDbType.VarChar ,256),
            };
            parameters[0].Value = UserName;
            parameters[1].Value = Password;
            parameters[2].Value = sessionid;

            DeliverInfo model = null;
            using (SqlDataReader dr = SQLHelper.ExecuteReader(CommandType.StoredProcedure, "GetDeliverModelByUserNameAndPassword", parameters))
            {
                if (dr.Read())
                {
                    model = new DeliverInfo();

                    model.DataId = HJConvert.ToInt32(dr["DataId"]);
                    model.CodeId = HJConvert.ToString(dr["CodeId"]);
                    model.Name = HJConvert.ToString(dr["Name"]);
                    model.Phone = HJConvert.ToString(dr["Phone"]);
                    model.Section = HJConvert.ToString(dr["Section"]);
                    model.Status = HJConvert.ToInt32(dr["Status"]);
                    model.GpsIMEI = HJConvert.ToString(dr["GpsIMEI"]);
                    model.OrderNum = HJConvert.ToInt32(dr["OrderNum"]);
                    model.UserName = HJConvert.ToString(dr["UserName"]);
                    model.Password = HJConvert.ToString(dr["Password"]);
                    model.Inve1 = HJConvert.ToInt32(dr["Inve1"]);
                    model.Inve2 = HJConvert.ToString(dr["Inve2"]);
                    model.AddDate = HJConvert.ToDateTime(dr["AddDate"]);

                }
            }

            return model;
        }
    }
}
